/**********************************************************************
 Copyright (C) 2009 Jaakko Vuori

 This file is part of FlexiWorker.
 For more information, see <http://code.google.com/p/flexi-worker/>

 FlexiWorker is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 FlexiWorker is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with FlexiWorker.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "flexiviewerwindow.h"
#include "ui_flexiviewerwindow.h"
#include "timelinewidget.h"
#include <QtGui>
#include <QDebug>

const int daysInView = 7;

FlexiViewerWindow::FlexiViewerWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::FlexiViewerWindowClass)
{
    QString testLogFile = QDir::toNativeSeparators("../viewer/test/test_log.log");
    if (QFile::exists(testLogFile))
    {
        //Hard coded starting date for test_log.log
        m_weekStartDate.setDate(2009, 12, 21);
    }
    else
    {
        QDate currentDate = QDate::currentDate();
        m_weekStartDate = currentDate.addDays(-(currentDate.dayOfWeek() - 1));
    }

    ui->setupUi(this);
    ui->layout->setColumnStretch(0, 0);
    ui->layout->setColumnStretch(1, 1);
    ui->layout->setColumnMinimumWidth(0, 100);
    ui->layout->setColumnMinimumWidth(2, 150);

    for (int i = 0; i < daysInView; ++i)
    {
        QDate date = m_weekStartDate.addDays(i);
        DayRow dayRow;

        TimeLineWidget* timeLineWidget = new TimeLineWidget(this);
        dayRow.m_timeLineWidget = timeLineWidget;

        QLabel* dateLabel = new QLabel(this);
        dayRow.m_dateLabel = dateLabel;

        QLabel* workTimeLabel = new QLabel("Joopajoo", this);
        dayRow.m_workTimeLabel = workTimeLabel;

        connect(timeLineWidget, SIGNAL(workingTimeAdjusted(TimeLineWidget*, TimeFrame)),
            this, SLOT(updateWorkingTime(TimeLineWidget*, TimeFrame)));

        ui->layout->addWidget(dateLabel, i, 0);
        ui->layout->addWidget(timeLineWidget, i, 1);
        ui->layout->addWidget(workTimeLabel, i, 2);

        m_dayRows.append(dayRow);
    }
    updateWeek();
}

FlexiViewerWindow::~FlexiViewerWindow()
{
    delete ui;
}

void FlexiViewerWindow::on_actionNextWeek_triggered()
{
    m_weekStartDate = m_weekStartDate.addDays(daysInView);
    updateWeek();
}

void FlexiViewerWindow::on_actionPreviousWeek_triggered()
{
    m_weekStartDate = m_weekStartDate.addDays(-daysInView);
    updateWeek();
}

void FlexiViewerWindow::updateDay(int dayInPeriod)
{
    TimeLineWidget* t = m_dayRows.at(dayInPeriod).m_timeLineWidget;
    QDate date = m_weekStartDate.addDays(dayInPeriod);
    t->setPresenceTimes(m_model.presenceTimes(date));
    t->setWorkingTimes(m_model.workingTimes(date));
    t->setDayStartTime(QDateTime(date, KDayStartTime));

    QLabel* label = m_dayRows.at(dayInPeriod).m_dateLabel;
    label->setText(date.toString("ddd ") + date.toString(Qt::SystemLocaleShortDate));

    QLabel* workTimeLabel = m_dayRows.at(dayInPeriod).m_workTimeLabel;
    if (m_model.workingTimes(date).count() > 0)
    {
        int totalSecs = 0;
        QString str;
        for (int i = 0; i < m_model.workingTimes(date).count(); ++i)
        {
            QDateTime startTime = m_model.workingTimes(date)[i].m_startTime;
            QDateTime endTime = m_model.workingTimes(date)[i].m_endTime;
            str.append(startTime.time().toString());
            str.append(" - ");
            str.append(endTime.time().toString());
            str.append(" = ");

            int secs = startTime.secsTo(endTime);
            totalSecs += secs;
            int hours = secs / 60 / 60;
            int mins = (secs - hours * 60 *60) / 60;
            str += QString::number(hours) + ":";
            if (abs(mins) < 10)
            {
                str += "0";
            }
            str += QString::number(mins);
            str += "\n";
        }

        int hours = totalSecs / 60 / 60;
        int mins = (totalSecs - hours * 60 *60) / 60;

        str += "TOTAL: ";
        str += QString::number(hours) + ":";
        if (abs(mins) < 10)
        {
            str += "0";
        }
        str += QString::number(mins);
        str += " - 30 min = ";
        totalSecs -= 30 * 60;    //Lunch time
        hours = totalSecs / 60 / 60;
        mins = (totalSecs - hours * 60 *60) / 60;

        str += QString::number(hours) + ":";
        if (abs(mins) < 10)
        {
            str += "0";
        }
        str += QString::number(mins);
        str += "\n -> ";

        qreal workHours = hours + qreal((mins + 15) * 2 / 60) / 2;
        str += QString::number(workHours) + " h";
        workTimeLabel->setText(str);
    }
    else
    {
        workTimeLabel->setText("");
    }
}

void FlexiViewerWindow::updateWeek()
{
    for (int i = 0; i < m_dayRows.count(); ++i)
    {
    updateDay(i);
    }
}

void FlexiViewerWindow::updateWorkingTime(TimeLineWidget* timeLineWidget, TimeFrame workingTime)
{
    qDebug() << "FlexiViewerWindow::updateWorkingTime" << timeLineWidget->dayStartTime().date() << workingTime.m_startTime;
    QDate date = timeLineWidget->dayStartTime().date();
    m_model.updateWorkingTime(date, timeLineWidget->highlightedWorkingTime(), workingTime);
    //timeLineWidget->setWorkingTimes(m_model.workingTimes(date));
    int day = m_weekStartDate.daysTo(date);
    updateDay(day);
    //m_dayRows.at(day).m_timeLineWidget->setWorkingTimes(m_model.workingTimes(date));
    //updateWeek();
}
